package com.data.structure.node;

/**
 * @author zflu
 * 优先队列
 * Created by lucky on 2018-12-10.
 */
public class PriorityQueue {

    private long[] a;

    private int size;

    private int nItems;

    public PriorityQueue(int maxSize) {
        this.size = maxSize;
        a = new long[size];
        nItems = 0;
    }

    public void insert(long v) {
        if (isFull()) {
            System.out.println("队列已满!");
            return;
        }
        int j;
        if (nItems == 0) {
            a[nItems++] = v;
        } else {
            //排序
            for (j = nItems - 1; j >= 0; j--) {
                if (v > a[j]) {
                    a[j + 1] = a[j];
                } else {
                    break;
                }
            }
            a[j + 1] = v;
            nItems++;
        }
    }

    public long remove() {
        if (isEmpty()) {
            System.out.println("队列为空!");
            return 0;
        }
        return a[--nItems];
    }

    public long peekMin() {
        if (isEmpty()) {
            System.out.println("队列为空!");
            return 0;
        }
        return a[nItems - 1];
    }

    public void display() {
        if (isEmpty()) {
            System.out.println("队列为空!");
            return;
        }
        for (int i = 0; i < nItems; i++) {
            System.out.println(a[i]);
        }
    }

    public boolean isEmpty() {
        return (nItems == 0);
    }

    public boolean isFull() {
        return (size == nItems);
    }

    public static void main(String[] args) {
        PriorityQueue queue = new PriorityQueue(5);
        queue.insert(1);
        queue.insert(2);
        queue.insert(3);
        queue.insert(4);
        queue.insert(5);
        System.out.println(queue.peekMin());
        System.out.println(queue.peekMin());
        queue.remove();
        queue.display();
    }
}
